﻿/// <reference path="../GeneratedArtifacts/viewModel.js" />

// Value to hold the cached OrderDetailRowParent
var OrderDetailRowParent;
myapp.AddEditOrder.RowTemplate1_postRender = function (element, contentItem) {
    // Cache the OrderDetail item so it can possibly be updated
    OrderDetailRowParent = $(element).parent();
    // Get Order Quantity
    var OrderQuantity = contentItem.data.OrderQuantity;
    if (OrderQuantity != null && OrderQuantity != undefined) {
        // If we have a value -- color the line item
        ColorOrderDetail(OrderQuantity);
    }

    // Update the cached order when one is selected
    $(element).on("click", function () {
        // Cache the OrderDetail item so it can possibly be updated
        OrderDetailRowParent = $(element).parent();
    });
};

// Utility
function ColorOrderDetail(OrderQuantity) {
    // if OrderQuantity is more than 5 make the background Blue
    // otherwise make it Yellow
    if (OrderQuantity > 5) {
        OrderDetailRowParent.css({
            "background-color": "blue",
            "background-image": "none",
            color: "white"
        });
    }
    else {
        OrderDetailRowParent.css({
            "background-color": "yellow",
            "background-image": "none",
            color: "black"
        });
    }
}

myapp.AddEditOrder.DynamicAddOrderDetail_execute = function (screen) {

    myapp.showAddEditOrderDetail(null, {

        beforeShown: function (AddEditOrderDetailScreen) {
            // Make a new OrderDetail
            var newOrderDetail = new myapp.OrderDetail();
            // Set the Order property
            newOrderDetail.setOrder(screen.Order);
            // Set the Quantity
            newOrderDetail.OrderQuantity = 0;

            // Try to find a Product
            screen.details.dataWorkspace.ApplicationData.Products
                .load().then(function (results) {
                    // Try to get the first Product
                    var FirstProduct = results.results[0];
                    // Did we find a first Product?
                    if (FirstProduct != undefined && FirstProduct != null) {
                        // Set the first Product as the Product for the OrderDetail
                        newOrderDetail.setProduct(FirstProduct);
                    }

                    AddEditOrderDetailScreen.OrderDetail = newOrderDetail;
                });
        },

        afterClosed: function (AddEditOrderDetailScreen, navigationAction) {
            // Update the last cached OrderDetailRowParent item 
            ColorOrderDetail(AddEditOrderDetailScreen.OrderDetail.OrderQuantity);
        }
    });
};

myapp.AddEditOrder.DynamicEditOrderDetail_execute = function (screen) {
    // Open the AddEditOrderDetail screen 
    // Passing it the currently selected OrderDetails item
    myapp.showAddEditOrderDetail(screen.OrderDetails.selectedItem, {
        afterClosed: function (AddEditOrderDetailScreen, navigationAction) {
            // Update the last cached OrderDetailRowParent item 
            ColorOrderDetail(AddEditOrderDetailScreen.OrderDetail.OrderQuantity);
        }
    });
};